﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace web.Pages.xml
{
    public partial class IndicatorList : System.Web.UI.Page
    {
        protected List<LabcheckInfo> IndicatorDataList; 
        
        protected PatientRecordInfo PrData;
        #region 用户基本信息
        protected UserInfo UserData = new UserInfo();// 当前用户的信息
        #endregion

        protected int DiseaseId = 0;

        protected void ResponseContext(int iCode, string sMsg)
        {
            //生产xml文档
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            Response.ContentType = "text/xml";
            using (XmlWriter wr = XmlWriter.Create(Response.OutputStream, settings))
            {
                // Write XML data.
                wr.WriteStartElement("result");
                wr.WriteAttributeString("code", iCode.ToString());
                wr.WriteAttributeString("msg", sMsg);
                if (iCode == 0)
                {
                    wr.WriteStartElement("Indicators");
                    wr.WriteAttributeString("count", IndicatorDataList.Count.ToString());
                    foreach (LabcheckInfo info in IndicatorDataList)
                    {
                        wr.WriteStartElement("Indicator");
                        wr.WriteAttributeString("id", info.Id.ToString());
                        wr.WriteAttributeString("name", info.Name.ToString());
                        wr.WriteAttributeString("measurementunits", info.MeasurementUnits);
                        wr.WriteEndElement();
                    }
                }

                wr.WriteEndElement();
                wr.WriteEndElement();
                wr.Flush();
            }
        }

        protected bool CheckLogin()
        {
            UserData.Id = CookiesTools.GetUserIdCookie();
            if (UserData.Id > 0) //表示永久登陆的用户
            {
                UserData.UserName = CookiesTools.GetUserNameCookie();   //直接从cookie中获取用户名
                if (UserData.UserName.Length <= 0)
                {
                    UserData.Id = -1;    //如果不存在用户名，则为无效的cookie
                    return false;
                }
            }
            return true;
        }

        protected bool CheckParam(ref string sMsg)
        {
            return true;
        }

        protected void Handle()
        {
            if (!CheckLogin())
            {
                ResponseContext(-1000, "你还没有登陆");
                return;
            }
            string sErrMsg = "";
            if (!CheckParam(ref sErrMsg))
            {
                ResponseContext(-1001, sErrMsg);
                return;
            }

            PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
            if (PrData.Id <= 0)
            {
                ResponseContext(-100, "请先填写患病基本信息");
                return;
            }

            //直接从病人数据中取出DiseaseId
            IndicatorDataList = LabcheckBLL.Instance.GetLabcheckList(PrData.DiseaseId);

            ResponseContext(0, "成功");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Handle();
        }
    }
}
